
package com.android.fitnesscenter.download;

import java.util.HashMap;
import java.util.Map;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

/**
 * 文件下载业务bean
 */
public class FileService {
    private DBOpenHelper openHelper;

    public FileService(Context context) {
        openHelper = new DBOpenHelper(context);
    }

    /**
     * 获取每条线程已经下载的文件长度
     * 
     * @param path
     * @return
     */
    @SuppressLint("UseSparseArrays")
    public Map<Integer, Integer> getData(String path) {
        SQLiteDatabase db = openHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery("select threadid, downlength,need_downlength from filedownlog where downpath=?",
                new String[] {
                    path
                });
        try {
            Map<Integer, Integer> data = new HashMap<Integer, Integer>();
            while (cursor != null && cursor.moveToNext()) {
                data.put(cursor.getInt(0), cursor.getInt(1));
            }
            return data;
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            if (db != null) {
                db.close();
            }
        }
    }

    /**
     * 保存每条线程已经下载的文件长度
     * 
     * @param path
     * @param map
     */
    public void save(String path, Map<Integer, Integer> map) {// int threadid,
                                                              // int position
        SQLiteDatabase db = openHelper.getWritableDatabase();
        try {
            db.beginTransaction();
            for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
                db.execSQL("insert into filedownlog(downpath, threadid, downlength) values(?,?,?)",
                        new Object[] {
                                path, entry.getKey(), entry.getValue()
                        });
            }
            db.setTransactionSuccessful();
        } finally {
            if (db != null) {
                db.endTransaction();
                db.close();
            }
        }
    }

    /**
     * 实时更新每条线程已经下载的文件长度
     * 
     * @param path
     * @param map
     */
    public void update(String path, Map<Integer, Integer> map) {
        SQLiteDatabase db = openHelper.getWritableDatabase();
        try {
            db.beginTransaction();
            for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
                db.execSQL("update filedownlog set downlength=? where downpath=? and threadid=?",
                        new Object[] {
                                entry.getValue(), path, entry.getKey()
                        });
            }
            db.setTransactionSuccessful();
        } finally {
            if (db != null) {
                db.endTransaction();
                db.close();
            }
        }
    }

    /**
     * 当文件下载完成后，删除对应的下载记录
     * 
     * @param path
     */
    public void delete(String path) {
        SQLiteDatabase db = openHelper.getWritableDatabase();
        try {
            db.execSQL("delete from filedownlog where downpath=?", new Object[] {
                    path
            });
        } finally {
            if (db != null) {
                db.close();
            }
        }
    }

}
